package com.cosina.game.crystallight.ui.mapselect;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout.LayoutParams;

import com.cosina.game.crystallight.MediaPlayerAdapter;
import com.cosina.game.crystallight.model.battle.BattleFieldProxy;
import com.cosina.game.crystallight.model.battle.BattleFieldProxyManager;
import com.cosina.game.crystallight.ui.ResourceManager;
import com.cosina.game.crystallight.ui.common.PaintSuite;
import com.cosina.game.crystallight.util.CanvasUtil;

public class TollGateSelectView extends View {
	public static final int TITLEHEIGHT =  147;
	private final UpAndDownLayout upAndDownLayout;
	
	public TollGateSelectView(Context context,UpAndDownLayout upAndDownLayout) {
		super(context);
		this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, UpAndDownLayout.UP_HEIGHT));
		this.upAndDownLayout =  upAndDownLayout;
		mapTitleBitmap = ResourceManager.get("img/map/map_title.jpg");
		playTollGateBitmap  = ResourceManager.get("img/map/play_cmp_dlg.jpg");
	}

	public int tollGateId = -1;
	Bitmap mapTitleBitmap, playTollGateBitmap;
	
	
	private static final int scoreImageLeftX = 129,scoreImageLeftY = 91 + TITLEHEIGHT;
	private static final int scoreImageRightX = 141,scoreImageRightY = 91 + TITLEHEIGHT;
	
	private static final int castleX = 38, castleY = 35 + TITLEHEIGHT;
	private static final int castleNameX = 190, castleNameY = 36 + TITLEHEIGHT;
	private static final int passedTimeX = 152, passedTimeY = 140 + TITLEHEIGHT;
	private static final int waveX = 214, waveY = 176 + TITLEHEIGHT;
	private static final int scoreX = 264, scoreY = 77 + TITLEHEIGHT;
	private static final RectF playButtonRect = new RectF(57,203 + TITLEHEIGHT,144,251 + TITLEHEIGHT);
	private static final RectF backButtonRect = new RectF(177,203 + TITLEHEIGHT,264,251 + TITLEHEIGHT);
	
	private Paint paint4Text = new Paint(PaintSuite.p4Text);
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawBitmap(mapTitleBitmap, 0,0,PaintSuite.p4Simple);
		canvas.drawBitmap(playTollGateBitmap, 0,147,PaintSuite.p4Simple);
		BattleFieldProxy tollGate = BattleFieldProxyManager.get(tollGateId);
		
		Bitmap bitmap = null;
		if(tollGate.style == BattleFieldProxy.FLAT_TOP_STYLE){
			bitmap = ResourceManager.get("img/map/castle_flat_top.png");
		}else{
			bitmap = ResourceManager.get("img/map/castle_round_top.png");
		}
		canvas.drawBitmap(bitmap, castleX, castleY, PaintSuite.p4Simple);
		
		paint4Text.setTextSize(21);
		{
			String passedTime = "0";
			CanvasUtil.drawText(canvas, passedTime, passedTimeX, passedTimeY, paint4Text, 21);
		}
		{
			CanvasUtil.drawText(canvas, tollGate.name, castleNameX, castleNameY, paint4Text, 21);
		}
		{
			String wave = tollGate.getWaveCount() + "";
			CanvasUtil.drawText(canvas, wave, waveX, waveY, paint4Text, 21);
		}
		{
			String bestScore = "0";
			CanvasUtil.drawText(canvas, bestScore, scoreX, scoreY, paint4Text, 21);
		}
		
		{
			Bitmap leftCap = ResourceManager.get("img/map/prog_l.png");
			Bitmap rightCap = ResourceManager.get("img/map/prog_r.png");
			canvas.drawBitmap(leftCap, scoreImageLeftX, scoreImageLeftY, PaintSuite.p4Simple);
			canvas.drawBitmap(rightCap, scoreImageRightX, scoreImageRightY, PaintSuite.p4Simple);
		}
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_UP){
			float x = event.getX();
			float y = event.getY();
			if(playButtonRect.contains(x, y)){
				MediaPlayerAdapter.menuBtnclick();
				upAndDownLayout.playTollGate(tollGateId);
			}else if(backButtonRect.contains(x, y)){
				MediaPlayerAdapter.menuBtnclick();
				upAndDownLayout.showMap();
			}
		}
		return true;
	}
	
	
}
